package com.gy.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

/**
 * 读取hdfs伤的文件
 */
public class fileReadFromHdfs {

    public static void main(String[] args) {
        String srcFile = "hdfs://gyClusterHa/tmp/test/input/word";
        String destination = "/Users/mac/Downloads/workspace/kkb-bigdata/src/main/resources/test.txt";
        try {
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(URI.create(srcFile), conf);
            FSDataInputStream in = fs.open(new Path(srcFile));
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination));
            IOUtils.copyBytes(in, out, 1024, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
